templatePath = 'template-path/'
angular.module('UCX.Cmis')
.controller "AclCtrl", ($scope, $uibModal, CmisService, $timeout)->
  CmisServiceSession = CmisService.session
  objectId = $scope.choseFile.id
  $scope.createACL = ()->
    modalInstance = $uibModal.open {
      animation: true,
      templateUrl: templatePath + "create-acl.html",
      controller: 'CreateAclCtrl',
      backdrop: 'static'
    }
    modalInstance.result.then (data)->
      ace = []
      ace[data.name] = [data.permission]
      CmisServiceSession.applyACL(objectId, ace).$ok $scope, (data)->
        $scope.choseFile.getAces ()->
          $scope.choseFile.getAces ()->
            $timeout ()-> $scope.$apply()
          ,
            ()->
          ,
            true

  $scope.removeACL = (index)->
    ace = $scope.choseFile.aces[index]
    aceData = []
    aceData[ace.principal.principalId] = [ace.permissions[0]]
    CmisServiceSession.applyACL(objectId, null ,aceData).$ok $scope, (data)->
      $scope.choseFile.getAces ()->
        $timeout ()-> $scope.$apply()
      ,
        ()->
      ,
        true

.controller "CreateAclCtrl", ($scope, $uibModalInstance)->
  $scope.data = {
    name : '',
    permission : 'cmis:read'
  }
  $scope.ok = ()->
    $uibModalInstance.close($scope.data)
  $scope.cancel = ()->
    $uibModalInstance.dismiss('cancel')
.controller 'version', ($scope, CmisService, $timeout)->
  CmisServiceSession = CmisService.session
  $scope.deleteVersion = (index)->
    currentObjectId =
      $scope.choseFile.versions[index].succinctProperties['cmis:objectId']
    CmisServiceSession.deleteObject(currentObjectId).ok ()->
      $scope.choseFile.getVersions ()->
        if $scope.choseFile.versions.length == 0
          $scope.choseFile = null
        $timeout ()-> $scope.$apply()
      ,
        null
      ,
        true
.controller 'DropdownNavbarCtrl', ($scope)->
  $scope.exitRoom = ()->
    console.log 'exitRoom'
    if window.exitRoom && typeof window.exitRoom == 'function'
      window.exitRoom()
  $scope.editRoom = ()->
    console.log 'editRoom'
    if window.editRoom && typeof window.editRoom == 'function'
      window.editRoom()

.controller 'FileDetailCtrl', ($scope)->
  $scope.refreshComment = ()->
    if $scope.choseFile && $scope.choseFile.selectTab == 'comment'
      $scope.choseFile.noMoreComments=false
      $scope.choseFile.loadComment(true)
  $scope.refreshActivity = ()->
    if $scope.choseFile && $scope.choseFile.selectTab == 'activity'
      $scope.choseFile.noMoreActivities=false
      $scope.choseFile.loadActivity(true)
  #    commentContainer 是滚动条的那个元素id
  $scope.activitiesContainer='#scrollEle'
  $scope.commentContainer='#file-detail'
  $scope.loadMoreActivities=()->
    if $scope.choseFile && $scope.choseFile.selectTab == 'activity' && $scope.choseFile.activities
      timestamp = $scope.choseFile.activities[$scope.choseFile.activities.length-1].microtime
      #      api 里面直接传  时间有问题 传 microtime ok 所以转换下
      $scope.choseFile.loadActivity(timestamp)

  $scope.loadMoreComments=()->
    if $scope.choseFile && $scope.choseFile.selectTab == 'comment' && $scope.choseFile.comments
#      console.log ModelService.getRoomId()
      timestamp = $scope.choseFile.comments[$scope.choseFile.comments.length-1].create_time
      $scope.choseFile.loadComment(timestamp)

.controller 'LanguageCtrl', ($scope, $translate, localStorageService, Utils)->
  $scope.language = Utils.getLanguage()
  $translate.use $scope.language
  $scope.changeLanguage = (lang)->
    $scope.language = lang
    $translate.use lang
    localStorageService.set "language", lang
